package apps.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.web.servlet.ModelAndView;

import webframe.common.dao.Pagination;
import webframe.common.helper.ControllerHelper;
import apps.web.controller.base.MyBaseCommandController;
import apps.web.model.Apply;
import apps.web.model.Decl;

public class ExhDeclSearchController extends MyBaseCommandController {
	private static Order[] orders = new Order[] { Order.desc("id") };

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command) {
		Decl cdecl = (Decl) command;
		Apply apply = getServiceContainer().getApplyBizService().findApplyById(
				cdecl.getApplyId());

		DetachedCriteria criteria = DetachedCriteria.forClass(Decl.class, "d");

		applyCriteria(criteria, cdecl);

		Map map = new HashMap();
		Pagination<Decl> page = ControllerHelper.queryPaginationWithSql(
				request, orders, criteria);

		map.put("page", page);
		map.put("cdecl", cdecl);
		map.put("apply", apply);

		return new ModelAndView("ExhDeclList.jsp", "map", map);
	}

	// 应用查询条件
	public static void applyCriteria(DetachedCriteria criteria, Decl cdecl) {
		ControllerHelper.addEqualRestrictions(criteria, "id", cdecl.getId());
		ControllerHelper.addLikeRestrictions(criteria, "declNo", cdecl
				.getDeclNo());
		ControllerHelper.addEqualRestrictions(criteria, "applyId", cdecl
				.getApplyId());

		// ControllerHelper.addEqualRestrictions(criteria,
		// "colligateResult",
		// cdecl.getColligateResult());

		// 监管结果
		// if (ControllerHelper.isSafe(cdecl.getMarkNo())) {
		// ControllerHelper.addEqualRestrictions(criteria.createAlias(
		// "supervisePlan", "sp"), "sp.result", Long.parseLong(cdecl
		// .getMarkNo()));
		// }
		// return criteria;
	}

}
